﻿using AutoMapper;
using Hotel.Common;
using Hotel.Domain;
using Hotel.IRepository;
using Hotel.IServices;
using Hotel.IServices.RequestDTO;
using Hotel.IServices.ResponseDTO;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hotel.Services
{
    public class HotelServices : IHotelServices
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger _logger;

        /// <summary>
        /// AutoMapper
        /// </summary>
        private readonly IMapper _mapper;

        private readonly IHotelTestRepository _hotelTestRepository;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="mapper"></param>
        public HotelServices(ILogger<HotelServices> logger, IMapper mapper, IHotelTestRepository hotelTestRepository)
        {
            _logger = logger;
            _mapper = mapper;
            _hotelTestRepository = hotelTestRepository;
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult> InsertDatas(InsertDatasRequestDTO param)
        {
            try
            {
                var apiresult = new ApiResult("新增数据失败");

                var entity = _mapper.Map<Hotel.Domain.HotelTest>(param);

                var result = await _hotelTestRepository.InsertDataAsync(entity);

                if (result) apiresult.Success();

                return apiresult;
            }
            catch (Exception ex)
            {
                _logger.LogError("InsertDatasErros:" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<ApiResult> UpdateDatas(UpdateDatasRequestDTO param)
        {
            try
            {
                var apiresult = new ApiResult("更新数据失败");



                var entity = await _hotelTestRepository.GetAsync(param.Id);

                if(entity.Id==0)
                {
                    apiresult.msg = "数据不存在";
                    apiresult.code = StatusCodeEnum.DataNotExist;
                    return apiresult;
                }

                entity = _mapper.Map(param, entity);


                var result = await _hotelTestRepository.UpdateDataAsync(entity);

                if (result) apiresult.Success();

                return apiresult;
            }
            catch (Exception ex)
            {
                _logger.LogError("UpdateDatasErros:" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取单条数据
        /// </summary>
        /// <returns></returns>
        public async Task<ApiResult> GetData(int id, string? value)
        {
            try
            {
                var apiresult = new ApiResult<GetDataResponseDTO>("获取数据失败");

                var result = await _hotelTestRepository.GetAsync(id);
                var value_result = await _hotelTestRepository.GetAsync(x => x.FirstValue == value);

                if (result.Id == 0)
                {
                    apiresult.msg = "数据不存在";
                    apiresult.code = StatusCodeEnum.DataNotExist;
                    return apiresult;
                }

                var data = _mapper.Map<GetDataResponseDTO>(result);

                apiresult.Success(data);

                return apiresult;
            }
            catch (Exception ex)
            {
                _logger.LogError("GetDatasErros:" + ex.Message);
                throw;
            }
        }

        //分页查询数据
        public async Task<ApiResult> GetDatas(GetDatasRequestDTO param)
        {
            try
            {
                var pagingdata = new PagingResult<IList<GetDataResponseDTO>>();
                var apiresult = new ApiResult<PagingResult<IList<GetDataResponseDTO>>>("获取数据失败");

                //select all
                var result = await _hotelTestRepository.GetAllAsync();

                var data = _mapper.Map<List<GetDataResponseDTO>>(result);

                pagingdata.data = data.Skip((param.PageIndex-1)*param.PageSize).Take(param.PageSize).ToList();
                pagingdata.totalCount = result.Count();
                pagingdata.totalPage = (int)Math.Ceiling((decimal)pagingdata.totalCount / param.PageSize);

                if (result != null) apiresult.Success(pagingdata);

                return apiresult;
            }
            catch (Exception ex)
            {
                _logger.LogError("GetDatasErros:" + ex.Message);
                throw;
            }
        }

    }
}
